package com.sunjob.mromessage.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.dev33.satoken.session.SaSession;
import cn.dev33.satoken.stp.StpUtil;
import com.sunjob.common.domain.loginServer.SysUser;
import com.sunjob.common.domain.message.SysNotice;
import com.sunjob.common.domain.message.SysNoticeReceive;
import com.sunjob.common.domain.message.SysNoticeSelector;
import com.sunjob.common.result.Result;
import com.sunjob.common.util.PageHelper;
import com.sunjob.mromessage.service.MroMessageService;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by IntelliJ IDEA.
 *
 * @Author : Wangzurong
 * @create 2022-08-05 15:15
 */

@RestController
@Api(tags = "通知公告api",value = "通知公告api")
public class MroMessageController {
    @Autowired
    MroMessageService messageService;


    @SaCheckPermission("system:notice:push")
    @PostMapping("/pushNoticeBySelector")
    @ApiOperation(value = "通知公告推送")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "sysNoticeSelector",value = "通知公告实体",paramType = "header"),
            @ApiImplicitParam(name = "satoken",value = "token",paramType = "header")
    })
    Result<Boolean> pushNoticeBySelector(@RequestBody SysNoticeSelector sysNoticeSelector) {
        boolean res = messageService.pushNoticeBySelector(sysNoticeSelector);
        if(!res){
            return Result.fail("推送失败");
        }
        return Result.ok(res,"推送成功");
    }

    @ApiOperation(value = "拉取通知")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "startPage",value = "起始页码",paramType = "path"),
            @ApiImplicitParam(name = "pageSize",value = "页记录数",paramType = "path"),
            @ApiImplicitParam(name = "pageNum",value = "总页数",paramType = "path"),
            @ApiImplicitParam(name = "type",value = "消息类型",paramType = "path"),
            @ApiImplicitParam(name = "satoken",value = "token",paramType = "header")
    })

    @GetMapping("/pullNoticeByPage/{startPage}/{pageSize}/{pageNum}/{type}")
    Result<List<List<SysNotice>>> pullNoticeByPage(@PathVariable("startPage") Integer startPage, @PathVariable("pageSize") Integer pageSize, @PathVariable("pageNum") Integer pageNum, @PathVariable("type") Integer type) {
        Long userId = null;
        try {
            userId= StpUtil.getLoginIdAsLong();

        }
        catch (Exception e){
            return null;
        }
        List<SysNotice> res = messageService.pullNoticeByUserId(userId,type);
        PageHelper<SysNotice> pageHelper = new PageHelper<>(res);
        return Result.ok(pageHelper.getPageInfo(startPage,pageSize,pageNum),"查询通知公告成功");
    }


    @ApiOperation(value = "查看所有通知和公告")
    @GetMapping("/findAllNotice")
    public List<SysNotice> findAllNotice(){
        return messageService.findAllNotice();
    }

    @ApiOperation(value = "查看所有公告")
    @GetMapping("/findAllAnnouncement")
    public List<SysNotice> findAllAnnouncement(){
        return messageService.findAllAnnouncement();
    }

    @ApiOperation(value = "查看所有通知")
    @GetMapping("/findAllInform")
    public List<SysNotice> findAllInform(){
        return messageService.findAllInform();
    }

    @ApiOperation(value = "通过id查找通知或公告")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "noticeId", value = "消息的id", paramType = "path")
    })
    @GetMapping("/queryAnnouncementOrInformById/{noticeId}")
    public SysNotice queryAnnouncementOrInformById(@PathVariable("noticeId") long noticeId){
        return messageService.queryAnnouncementOrInformById(noticeId);
    }

    @ApiOperation("通过创建者查询通知和公告")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "Creator", value = "消息创建者", paramType = "path")
    })
    @GetMapping("/queryAnnouncementOrInformByCreator/{Creator}")
    public List<SysNotice> queryAnnouncementOrInformByCreator(@PathVariable("Creator") String Creator) {
        return messageService.queryAnnouncementOrInformByCreator(Creator);
    }

    @ApiOperation("通过id删除通知或公告")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "noticeId", value = "通知或公告的id", paramType = "path")
    })
    @GetMapping("/deleteNoticeById/{noticeId}")
    public void deleteNoticeById(@PathVariable("noticeId") long noticeId) {
        messageService.deleteNoticeById(noticeId);
    }

    @ApiOperation("更新通知或公告，noticeId不为空、noticeTitle、noticeType、noticeContent、updateBy、updateTime不都为空")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "sysNotice", value = "sysNotice实例", paramType = "body")
    })
    @GetMapping("/updateNotice")
    public void updateNotice(@RequestBody SysNotice sysNotice) {
        /*sysNotice = new SysNotice();
        sysNotice.setNoticeId((long)2);
        sysNotice.setNoticeContent("上吧可达鸭");
        System.out.println(sysNotice);*/
        messageService.updateNotice(sysNotice);
    }


    @ApiOperation("receiveNoticeId不为空、noticeId、receiveType、receiveId、updateBy、updateTime、不都为空")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "sysNoticeReceive", value = "消息接收记录实体", paramType = "body")
    })
    @GetMapping("/updateNoticeReceiveBy")
    public void updateNoticeReceiveBy(@RequestBody SysNoticeReceive sysNoticeReceive) {
        /*sysNoticeReceive.setReceiveNoticeId((long)1);
        sysNoticeReceive.setUpdateBy("pi");*/
        messageService.updateNoticeReceiveBy(sysNoticeReceive);
    }

}
